import {
  conbineHolderList
} from '../../config/apiServe/new-rpc.js'
export default {
  data() {
    return {
      holderInfo: {},
      HolderList: [],
      listParam: [{
        page: {
          number: 1,
          size: 10
        },
        "sort": [{
          "key": "balance",
          "ascending": false
        }],
        range: [{
          key: 'balance',
          gt: 0
        }],
        "match": [{
          "value": this.$route.query.TokenName,
          "key": "asset_symbol"
        }]
      }],
      pages: {
        number: 1,
        size: 10
      }
    }
  },
  computed: {
    token() {
      return this.$route.query.TokenName
    },
    tokenList() {
      return this.$store.state.tokenList
    }
  },
  watch: {
    tokenList(val) {
      if (val.length > 0) {
        this.init()
      }
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.getHolderCount()
      this.getHolderList(this.pages)
    },
    // 获取持有人总数
    async getHolderCount() {
      this.tokenList.forEach(item => {
        if (this.token === item.symbol) {
          this.holderInfo = item
        }
      })
    },
    // 获取持有人列表
    async getHolderList(pages) {
      this.listParam[0].page = pages
      let list = await conbineHolderList(this.listParam)
      if (list.error === null) {
        console.log(list)
        let total = this.holderInfo.total
        this.HolderList = list.result
        this.HolderList.forEach((item, index) => {
          item.rank = index + (this.pages.number - 1) * this.pages.size + 1
          item.percent = (item.balance * 100 / total).toFixed(8).slice(0, -6)
        })
      }
    },
    pageChange(page) {
      this.pages.number = page;
      this.getHolderList(this.pages)
    },
    sizeChange(size) {
      this.pages.size = size;
      this.pages.number = 1;
      this.getHolderList(this.pages)
    }
  }
}